const app = getApp()
const ajax = app.myRequest()
import storage from "../../utils/storage.js"
Page({
    data: {
        list: [
            { nickname: '待邀请', 'avatar': '/images/activity/daiyaoqing.png' },
            { nickname: '待邀请', 'avatar': '/images/activity/daiyaoqing.png' },
            { nickname: '待邀请', 'avatar': '/images/activity/daiyaoqing.png' },
            { nickname: '待邀请', 'avatar': '/images/activity/daiyaoqing.png' },
            { nickname: '待邀请', 'avatar': '/images/activity/daiyaoqing.png' },
            { nickname: '待邀请', 'avatar': '/images/activity/daiyaoqing.png' },
            { nickname: '待邀请', 'avatar': '/images/activity/daiyaoqing.png' },
        ],
        user: null,
        shareCode_: null,
        discount: null
    },

    onLoad: function (options) {
        var _this = this
        // 获取个人信息
        ajax.getRequest('/User/profile').then((res) => {
            _this.setData({ user: res.data.data })
        })

        // 获取优惠券
        ajax.getRequest('/User/getDiscount').then((res) => {
            _this.setData({ discount: res.data.data })
            if (res.data.code == 1) {
                for (let i of res.data.data) {
                    if (i.type == 1) {
                        _this.setData({ zhouka: true })
                    }
                    if (i.type == 2) {
                        _this.setData({ yueka: true })
                    }
                }
            }
        })

        // 邀请记录
        ajax.getRequest('/User/FriendHelpRecord').then((res) => {
            if (res.data.code == 1) {
                let list = res.data.data
                let list_ = _this.data.list
                for (let i = 0; i < list_.length; i++) if (list[i]) list_[i] = list[i]
                _this.setData({ list: list_ })
            }
            wx.stopPullDownRefresh()
        })
        this.start()
    },

    // 下拉刷新
    onPullDownRefresh: function () {
        this.onLoad()
    },

    // 领取三日卡 订阅三日卡过期小程序通知
    lingqusanrika: function () {
        let that = this
        wx.requestSubscribeMessage({
            tmplIds: ['B77QTMz8gwX85HzRvPb8FXVSDDD21ETcXoSYB0YSJkc'],
            success(res) {
                if (res['B77QTMz8gwX85HzRvPb8FXVSDDD21ETcXoSYB0YSJkc'] === 'accept') {
                    ajax.getRequest('/User/newUser').then((res) => {
                        wx.showToast({ title: res.data.msg })
                        ajax.getRequest('/User/profile').then((res) => {
                            that.setData({ user: res.data.data })
                        })
                    })
                }
            }
        })
    },

    // 授权手机号
    bindgetphonenumber: function (e) {
        if (e.detail.errMsg === "getPhoneNumber:ok") {
            let that = this
            let encryptedData = e.detail.encryptedData
            let iv = e.detail.iv
            ajax.getRequest('/User/authPhone', { encryptedData, iv }).then((res) => {
                storage.setStorage('mobile', res.data.data)
                ajax.getRequest('/User/profile').then((res) => {
                    that.setData({ user: res.data.data })
                })
            })
        }
    },

    // 面对面
    mianduimian: function () {
        let that = this
        if (that.data.shareCode_ == null) {
            var page = 'pages/activity/newUserHelp'
            var scene = that.data.user.id
            var width = 300
            ajax.postRequest('/Content/qrcode', { page, scene, width }).then((res) => {
                if (res.data.code == 1) {
                    that.setData({ shareCode_: res.data.data, shareCode: !0 })
                } else {
                    wx.showToast({ title: 'ServerError：' + res.data.msg, icon: 'none' })
                }
            })
        } else {
            that.setData({ shareCode: !0 })
        }
    },

    // 关闭弹窗
    closeShareCode: function () {
        this.setData({ shareCode: false })
    },

    // 查看三日卡可用场馆
    sanrikakeyong: function () {
        if (this.data.user.reward.status == 3) {
            wx.showModal({ showCancel: false, title: '提示', content: '您的三日卡已失效' })
        } else {
            wx.navigateTo({ url: '/pages/activity/newUserVenue?type=sanri' })
        }
    },

    // 查看优惠券可用场馆
    youhuikeyong: function (e) {
        let index = e.currentTarget.dataset.index
        if (this.data.discount[index].status != 0) {
            wx.showModal({ title: '提示', content: '您的优惠卡已使用，或已过期', showCancel: false })
        } else {
            wx.navigateTo({ url: '/pages/activity/newUserVenue?type=youhui' })
        }
    },

    // 动画
    start: function () {
        var attentionAnim = wx.createAnimation({
            duration: 500,
            timingFunction: 'ease',
            delay: 0
        })
        //设置循环动画
        this.attentionAnim = attentionAnim
        var next = true;
        setInterval(function () {
            if (next) {
                //根据需求实现相应的动画
                this.attentionAnim.opacity(0.9).scale(0.9, 0.9).step()
                next = !next;
            } else {
                this.attentionAnim.opacity(1).scale(1, 1).step()
                next = !next;
            }
            this.setData({
                //导出动画到指定控件animation属性
                attentionAnim: attentionAnim.export()
            })
        }.bind(this), 500)
    }
})